*****************************************
*Create baseline sample (JSCReg and SReg)
*****************************************

set more off
*use hiding variables from network analysis
use "Network Data/DUP_hiding_04012004.dta",clear
gen year=2004
append using "Network Data/DUP_hiding_11102006.dta"
replace year=2006 if year==.

*merge with data on presence of each firm in Delo, UP or both
merge m:1 okpo using "Prepared Data/Delo_UP_presence.dta"
drop _merge
sort okpo year

*assign political affiliation dummy: 1-blue, 2-orange, 3-gray
gen color=1 if  group=="Derkach" | group=="Energo" | group=="Franchuk" | group=="Interpipe" | group=="Khmelnytskyi" | group=="Kirpa" | group=="Kliuev" | group=="Kyiv-Seven" | group=="Old Donetsk" | group=="Radon" | group=="SCM" | group=="Tabachnyk" | group=="TAS" | group=="Ukrinterproduct" //14
replace color=2 if group=="Aval" | group=="Brinkford" | group=="Finansy i Kredyt" | group=="Ignatenko" | group=="Ivchenko" | group=="Omelchenko" | group=="Orlan" | group=="Rymaruk" | group=="Tretiakov" | group=="UkrPromInvest" | group=="Yushchenko" | group=="Zagreva" | group=="Pryvat" //15
replace color=3 if group=="Basis" | group=="Feldman" | group=="Intercontact"  | group=="ISD" | group=="Kahanovskyi" | group=="UkrSotsBank" | group=="UkrSybBank" | group=="double SCM/ISD" | group=="double ISD/Pryvat" //9

gen blue=1 if color==1
replace blue=0 if blue==.
gen orange=1 if color==2
replace orange=0 if orange==.
gen gray=1 if color==3
replace gray=0 if gray==.

*keep firms present in JSCReg or SReg
drop if present_JSCReg_SReg==0

*keep firms with sector and controls data
merge m:1 okpo using "Prepared Data/sector.dta"
keep if _merge==3
drop _merge
merge m:1 okpo  using "Prepared Data/econ.dta"
keep if _merge==3
drop _merge
merge m:1 okpo using "Prepared Data/privatized.dta"
keep if _merge==3
drop _merge
*add regions and IV data
merge m:1 okpo using "Prepared Data/IV.dta"
keep if _merge==3
drop _merge

*restrict sample to firms with economic data
keep if lemp!=. & tfp!=. & privatized!=. 

*Generate additional variables

*1. No oligarch in chain and distance to oligarch
gen distance=1/shortest_path_oligarch 
replace distance=0 if shortest_path_oligarch==.
gen mdistance=1-distance

gen no_oligarch_chain=1 if oligarch_in_chain==0
replace no_oligarch_chain=0 if oligarch_in_chain==1

*2. Change in foreign and offshore (for FD regressions)
gen time=1 if year==2004
replace time=2 if year==2006

*keep only those firms that are present in 2004
bysort okpo: egen mtime=min(time)
keep if mtime==1
*===>13 firms are available in 2006 only, drop them

*generate DFO and DOFF for 349 firms present in both years
tsset okpo time
gen dfo=foreign_in_chain-l.foreign_in_chain
gen doff=offshore-l.offshore
bysort okpo: egen max_dfo=max(dfo)
bysort okpo: egen max_doff=max(doff)
drop dfo doff time mtime 
rename max_dfo dfo
rename max_doff doff

save "Regressions Data/baseline0406.dta",replace

*drop firms with no owners in 2004
gen nown2004=1 if has_owners==0 & year==2004
bysort okpo: egen nown=max(nown2004)
drop if nown==1
save "Regressions Data/baseline0406_nown.dta",replace
